# -*-coding: Utf-8 -*-
# @File : student .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2024/5/7
from lib import CommonView, decorator
from db import Student, Course
from interface import StudentInterface


class StudentView(CommonView):
    def __init__(self):
        super().__init__()
        self.student_menu = '''
        =====================这里是学生功能菜单=====================
                                1.注册
                                2.登录
                                3.查看课程信息
                                4.选择课程
                                5.查看成绩
                                q:退出系统               
        ==========================菜单结束==========================
        '''
        self.student_dict = {
            1: 'register',
            2: 'login',
            3: 'check_course',
            4: 'select_course',
            5: 'check_score'
        }
        self.login_dict = {
            'username': '',
            'role': 'Student'
        }
        self.interface = StudentInterface()

    def register(self):
        return super().common_register(cls=Student, role='Student')

    def login(self):
        return super().common_login(cls=Student, role='Student', login_dict=self.login_dict)

    @decorator(tag='login')
    def check_course(self):
        username = self.login_dict.get('username')
        role_name = self.tools.role_name(role='Admin')
        print(f'欢迎 {role_name} {username} 使用查看课程功能!')
        Course.show_data_info()
        self.tools.log_info(tag='info', role="Student", username=username,
                            info=f'查看所有课程信息成功!')
        return True

    @decorator(tag='login')
    def select_course(self):
        username = self.login_dict.get('username')
        role_name = self.tools.role_name(role='Admin')
        print(f'欢迎 {role_name} {username} 使用选择课程功能!')
        # 【一】展示所有课程
        flag, name_dict = Course.chose_name()
        if not flag:
            self.tools.log_info(tag='warning', role="Student", username=username,
                                info=f'查看课程信息失败!原因是当前无课程开设!')
            return False
        # 【二】学生选择课程编号
        course_id = input("请输入选择的学校编号 :>>>> ").strip()
        # 【三】获取对应的课程对象
        course_obj = name_dict.get(course_id)
        # 【四】调用接口层进行校验
        flag = self.interface.select_course_interface(username=username, course_obj=course_obj)
        return flag

    @decorator(tag='login')
    def check_score(self):
        username = self.login_dict.get('username')
        role_name = self.tools.role_name(role='Admin')
        print(f'欢迎 {role_name} {username} 使用查看课程分数功能!')
        flag = self.interface.check_score_interface(username=username)
        return flag

